/**
 * Created by xiezhouyan on 15-1-16.
 */
var http = require("http"),
    config = require("../config"),
    HttpUtil = require("../lib/http-util");
exports.search = function (req, res) {
    var wd = req.query.wd;
    var page_size = req.query.page_size ? req.query.page_size : 10;
    var page_index = req.query.page_index ? req.query.page_index : 0;
    var params = {
        "wt": "json",
        "rows": page_size,
        "start": (page_index * page_size),
        "indent": true
    };
    if (wd) {
        params.q = wd;
        params.mm = 2;
        params.qf = "title";
        params.defType = "dismax";
    } else {
        params.q = "*:*";
    }
    HttpUtil.GET(config.solr.url, params, function (err, result) {
        if (err) {
            res.render("err");
        } else {
            var response = result.response;
            if (response) {
                response.page_index = page_index;
                response.page_size = page_size;
                response.wd = wd;
            }
            if (result.highlighting && response.docs && response.docs.length != 0) {
                response.docs.forEach(function (doc) {
                    var highlight = result.highlighting[doc.id].title[0];
                    doc.title = highlight;
                });
            }
            res.render("search", response);
        }
    })
}